package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
public class DishServiceImpl implements DishService {

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private DishFlavorMapper dishFlavorMapper;

    @Autowired
    private SetmealDishMapper setmealDishMapper;

    @Autowired
    private SetmealMapper setmealMapper;


    /**
     * 新增菜品及对应口味
     * @param dishDTO
     */
    @Transactional
    @Override
    public void sava(DishDTO dishDTO) {

        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO, dish);
        dishMapper.sava(dish);

        Long id = dish.getId();//获得新增菜品所生成的主键~id

        List<DishFlavor> flavors = dishDTO.getFlavors();//获取风味参数
        if (flavors != null && flavors.size() > 0) {
            flavors.forEach(dishFlavor -> {
                dishFlavor.setDishId(id);//将获得的菜品主鍵id赋给dishFlavor对象的dishId屬性
            });
        }

        dishFlavorMapper.saveFlavor(flavors);

    }

    /**
     * 菜品分页查询
     * @param dishPageQueryDTO
     * @return
     */
    @Override
    public PageResult pageQuery(DishPageQueryDTO dishPageQueryDTO) {
        PageHelper.startPage(dishPageQueryDTO.getPage(), dishPageQueryDTO.getPageSize());

        Page<DishVO> page = dishMapper.pageQuery(dishPageQueryDTO);

        return new PageResult(page.getTotal(), page.getResult());
    }

    /**
     *  菜品批量删除
     * @param ids
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteByIds(List<Long> ids) {
//        根据状态判断当前菜品是否能够删除
        for (Long id : ids) {
            Dish dish = dishMapper.getById(id);
            if (dish.getStatus() == StatusConstant.ENABLE) {
                throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
            }
        }

//        根据当前的菜品的关联状态来判断其是否能被删除~~~是否被套餐关联
        List<Long> setmealIds = setmealDishMapper.getSetmealIdsByDishIds(ids);
        if (setmealIds != null && setmealIds.size() > 0) {
            throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
        }

        for (Long id : ids) {
            dishMapper.deleteById(id);
//            删除菜品关联的风味
            dishFlavorMapper.deleteById(id);

        }


    }

    /**
     * 通過id查詢菜品
     * @param id
     * @return
     */
    @Override
    public DishVO getById(Long id) {
        Dish dish = dishMapper.getById(id); // 菜品數據

//        DishFlavor flavorById = dishFlavorMapper.getFlavorById(id);
        List<DishFlavor> list = dishFlavorMapper.getFlavorById(id);

        DishVO dishVO = new DishVO();

        BeanUtils.copyProperties(dish, dishVO);

        dishVO.setFlavors(list);

        return dishVO;

    }

    /**
     * 修改菜品
     * @param dishDTO
     */
    @Override
    @Transactional
    public void update(DishDTO dishDTO) {
        Dish dish = Dish.builder().build();
        BeanUtils.copyProperties(dishDTO,dish);

        List<DishFlavor> flavors = dishDTO.getFlavors(); //獲得菜品風味

        Long id = dishDTO.getId(); // 菜品的id

//         先修改菜品
        dishMapper.update(dish);

//        判斷菜品是否有風味
        if (flavors != null && flavors.size() > 0) {
            //        先刪除菜品風味
            dishFlavorMapper.deleteById(id);
        }

//        將菜品id賦給對應風味中的dish_id
        flavors.forEach(dishFlavor -> {
            dishFlavor.setDishId(id);
        });

//        再次增加菜品風味

        dishFlavorMapper.saveFlavor(flavors);
    }

    /**
     * 根據分類id查詢菜品
     * @param
     * @return
     */
    @Override
    public List<DishVO> selectById(Long categoryId) {
        List<DishVO> list= dishMapper.getByCategoryId(categoryId);

            return list;

    }

    /**
     * 根據名字查詢菜品
     * @param name
     * @return
     */
    @Override
    public List<DishVO> getByName(String name) {

        List<DishVO> list = dishMapper.getByName(name);

        return list;
    }

    /**
     * 条件查询菜品和口味
     * @param dish
     * @return
     */
    @Override
    public List<DishVO> listWithFlavor(Dish dish) {
        List<DishVO> dishVOList = dishMapper.getByCategoryId(dish.getCategoryId());

//        List<DishVO> dishVOList = new ArrayList<>();

        for (DishVO d : dishVOList) {

//            BeanUtils.copyProperties(d,dishVO);

            //根据菜品id查询对应的口味
            List<DishFlavor> flavors = dishFlavorMapper.getFlavorById(d.getId());

            d.setFlavors(flavors);

        }

        return dishVOList;
    }
}
